<?php
class Author extends CI_Controller{

	function __construct(){
		parent::__construct();
		$this->load->model("model_author","author");
		$this->load->model("model_user","user");
		$session_data = $this->session->userdata("logged_in");
		if($session_data){
			$this->_data['fullname'] = $session_data['fullname'];
		}else{
			redirect("/admin/login","refresh");
		}
	}
	function index(){		
		$this->show();
	}
	function show(){
		$limit =50;
		$config['base_url'] = '/admin/author/show';
		$config['total_rows'] = $this->author->get_total_author();
		$config['per_page'] = $limit;
		$config['uri_segment'] = 4;
		$config['num_links'] = 3; 
		$config['full_tag_open'] = "<p class='pagination'>";
		$config['full_tag_close'] = "</p>";
		$config['next_link'] = "Sau";
		$config['prev_link'] = "Trước";
		$config['last_link'] = "Cuối";
		$config['first_link'] = "Đầu";

		$this->pagination->initialize($config);
		$this->_data['keyword'] = isset($keyword)?$keyword:"";
        $this->_data["link"] = $this->pagination->create_links();
        $this->_data['start'] = ($this->uri->segment(4)+1);
        $this->_data['end'] = ($this->_data['start']+$limit)-1;
		$this->_data["total"] = $this->author->get_total_author();
		$this->_data["author"] = $this->author->get_all_author($config['per_page'],$this->uri->segment(4));
		$this->_data['template'] = "author/list_author";
		$this->load->view("index_admin",$this->_data);
	}

	function create(){
		$this->_data['template'] = "author/add_author";
		$this->load->view("index_admin",$this->_data);
	}

	function save(){
		if(isset($_POST)){
			$name_author = $this->input->post("name_author");
			$ngaysinh = $this->input->post("ngaysinh_author");
			$err = "";
			if(trim($name_author)==""){
				$err.="- Họ và tên không được để trống<br>";
			}else if($this->author->get_info_author($name_author)){
				$err.="- Tên tác giả đã tồn tại";
			}
			if($err){
				$this->_data['err'] = "Có lỗi xẩy ra:<br>".$err;				
				$this->_data['template'] = "author/add_author";
				$this->load->view("index_admin",$this->_data);
			}else{
				$data = array(
						"author_name" => $name_author,
						"author_ngaysinh" => datetoint($ngaysinh),
					);
				$this->author->insert($data);
				redirect("/admin/author",'refresh');
			}
		}
	}
	function edit(){
		$id = $this->uri->segment(4);
		$info_author = $this->author->get_info_author_by_id($id);
		$this->_data['id'] = $info_author['author_id'];
		if($info_author['author_ngaysinh']==0){
			$this->_data['ngaysinh'] = "";
		}else{
			$this->_data['ngaysinh'] = inttodate($info_author['author_ngaysinh']);
		}
		$this->_data['author_name'] = $info_author['author_name'];
		$this->_data['template'] = "author/edit_author";
		$this->load->view("index_admin",$this->_data);
	}
	function update(){
		$id = $this->uri->segment(4);
		$name_author = $this->input->post("name_author");
		$ngaysinh = $this->input->post("ngaysinh_author");
		$data = array(
			"author_name" => $name_author,
			"author_ngaysinh" => datetoint($ngaysinh),
		);
		$this->author->update($id,$data);
		redirect("/admin/author","refresh");
	}
	function delete(){
		$id = $this->uri->segment(4);
		$this->author->delete($id);
	}
	function search_author(){
		if($this->input->post()){
			$keyword = $this->input->post("keyword");
		}else{
			$keyword = $this->uri->segment(4);
		}
		$config['base_url'] = "/admin/author/search_author/".$keyword;
		$config['total_rows'] = $this->author->count_search($keyword);
		$config['per_page'] = 20;
		$config['uri_segment'] = 5;
		$config['num_links'] = 3; 
		$config['full_tag_open'] = "<p class='pagination'>";
		$config['full_tag_close'] = "</p>";
		$config['next_link'] = "Sau";
		$config['prev_link'] = "Trước";
		$config['last_link'] = "Cuối";
		$config['first_link'] = "Đầu";

		$this->pagination->initialize($config);
		$this->_data['keyword'] = isset($keyword)?$keyword:"";
        $this->_data["link"] = $this->pagination->create_links();
		$this->_data["total"] = $this->author->count_search($keyword);
		$this->_data["author"] = $this->author->search($keyword,$config['per_page'],$this->uri->segment(5));
		$this->_data['template'] = "author/list_author";
		$this->load->view("index_admin",$this->_data);
	}
}
?>